package com.narbeshuber.tablet.lasalara.listadapters;

import java.util.List;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.narbeshuber.tablet.lasalara.LasaLaraActivity;
import com.narbeshuber.tablet.lasalara.R;
import com.narbeshuber.tablet.lasalara.fragment.ChapterFragment;
import com.narbeshuber.tablet.lasalara.fragment.QuestionFragment;
import com.narbeshuber.tablet.lasalara.model.Chapter;

public class ChapterAdapter extends BaseAdapter{

	private LasaLaraActivity context;
	private List<Chapter> chapters;
	
	public ChapterAdapter(LasaLaraActivity context,	List<Chapter> chapters) {
		super();
		this.context = context;
		this.chapters = chapters;
	}
	
	public int getCount() {
		return chapters.size();
	}

	public Chapter getItem(int position) {
		return chapters.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(com.narbeshuber.tablet.lasalara.R.layout.chapterrow, null);
		}
		final Chapter chapter = getItem(position);
		TextView chapterName = (TextView)convertView.findViewById(com.narbeshuber.tablet.lasalara.R.id.chapterName);
		TextView chapterAuthor = (TextView)convertView.findViewById(com.narbeshuber.tablet.lasalara.R.id.chapterAuthor);
		TextView chapterPercentage = (TextView)convertView.findViewById(com.narbeshuber.tablet.lasalara.R.id.percentage);
		
		
		chapterName.setText(chapter.getTitle());
		if(LasaLaraActivity.ninjaMode){
			chapterName.setTextColor(Color.WHITE);
			chapterAuthor.setTextColor(Color.WHITE);
		} else {
			chapterName.setTextColor(Color.BLACK);
			chapterAuthor.setTextColor(Color.BLACK);
		}
		
		if (chapter.getAuthorName().equalsIgnoreCase("null")) {									
			chapterAuthor.setText(chapter.getAuthorEmail());
		}
		else {
			if(chapter.getDr().equalsIgnoreCase("true")){
				chapterAuthor.setText("Dr. " + chapter.getAuthorName() + ", " + chapter.getInstitution());
			} else {
				chapterAuthor.setText(chapter.getAuthorName() + ", " + chapter.getInstitution());
			}
		}	
		
		Button resetButton = (Button)convertView.findViewById(R.id.resetChapter);
		resetButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				chapter.setAnswered(0);
				context.getDbHelper().resetQuestions(chapter);
				notifyDataSetChanged();
				if(chapter.getChapterKey().equals(ChapterFragment.chapter.getChapterKey()) && !LasaLaraActivity.pageView){
					FragmentManager fragmentManager = (FragmentManager) context
					.getFragmentManager();
					FragmentTransaction fragmentTransaction = fragmentManager
							.beginTransaction();
					QuestionFragment questionFragment = new QuestionFragment(context, 
							context.getDbHelper().getAllQuestionsFromChapter(ChapterFragment.chapter.getChapterKey()));
					fragmentTransaction
							.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
					fragmentTransaction.replace(R.id.right, questionFragment);
					fragmentTransaction.commit();
				}
				Log.d("reseting chapter", Integer.toString(position));
			}
		});
		if(ChapterFragment.resetMode){
			resetButton.setVisibility(View.VISIBLE);
		} else {
			resetButton.setVisibility(View.GONE);
		}
		
		if(chapter.getTotalQuestions() != 0){
			chapterPercentage.setText(String.valueOf(chapter.calculatePercentage())+" %");
		} else {
			chapterPercentage.setText("");
		}
		
		if(ChapterFragment.chapter != null && ChapterFragment.chapter.getChapterKey().equals(chapter.getChapterKey())){
			if(LasaLaraActivity.ninjaMode) {
				convertView.setBackgroundColor(context.getResources().getColor(R.color.chapter_highlight_night));
			} else {
				convertView.setBackgroundColor(context.getResources().getColor(R.color.chapter_highlight));
			}
		} else {
			convertView.setBackgroundColor(Color.TRANSPARENT);
		}
		
		
		calculateProgress();	
		return convertView;
	}
	
	public Chapter getChapterByKey(String key){
		for(Chapter chapter: chapters){
			if(chapter.getChapterKey().equals(key)){
				return chapter;
			}
		}
		return null;
	}
	
	public void calculateProgress(){
		int answeredQuestions = 0;
		int totalQuestions = 0;
		for(Chapter chapter:chapters){
			answeredQuestions += chapter.getAnswered();
			totalQuestions += chapter.getTotalQuestions();
		}
		ProgressBar progressBar = (ProgressBar)context.findViewById(R.id.progressBook);
		int ansPercentage = (int)Math.round((double)answeredQuestions/(double)totalQuestions*100);		
		progressBar.setProgress(ansPercentage);
	}

}
